﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CourseManager.Model.Data;
using Ext.Net;

namespace CourseManager.View.Contents.Student
{
	public partial class ScheduleContent : System.Web.UI.Page
	{
		private static readonly object[] Data = new object[]
		                                        	{
		                                        		new object[] {"8:00-8:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"8:55-9:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"10:00-10:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"10:55-11:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"12:10-12:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"13:05-13:50", "", "", "", "", "", "", ""},
		                                        		new object[] {"14:10-14:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"15:05-15:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"16:00-16:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"16:55-17:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"18:00-18:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"18:55-19:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"19:50-20:35", "", "", "", "", "", "", ""},
		                                        	};

		private Model.Student student;
		private static string[] termInfo = { "秋季学期", "冬季学期", "春季学期", "夏季学期"};
		protected void Page_Load(object sender, EventArgs e)
		{
			//Session["UserId"] = "08122333";
			if (student == null)
			{
				if (Session["UserId"] == null)
				{
					Response.Redirect(Constants.ERROR_PAGE);
				}
				else
				{
					student = new Model.Student(Session["UserId"].ToString());
				}
			}

			if (!IsPostBack)
			{
				string term = TermDAO.getCurrentTerm();
				txtTermYear.Text = term.Substring(0, 4);
				txtTerm.Text = termInfo[int.Parse(term.Substring(4))];
			}
			if (!X.IsAjaxRequest)
			{
				UpdateSchedule();
			}
		}

		private void UpdateSchedule()
		{
			var buffer = new char[8 * 13];
			for (int i = 0; i < buffer.Length; ++i)
				buffer[i] = ' ';


			for (int i = 1; i <= 7; ++i)
			{
				for (int j = 0; j < 13; ++j)
				{
					((object[])Data[j])[i] = "";
				}
			}

			foreach (var course in student.CourseList)
			{
				foreach (var timeInfo in course.TimeInfoList)
				{
					for (var i = timeInfo.Start; i <= timeInfo.End; ++i)
					{
						((object[])Data[i])[timeInfo.Day] = course.Tag;
						if (course.Tag != null)
						{
							buffer[timeInfo.Day * 13 + i] = char.Parse(course.Tag.ToString());
						}
					}
				}
			}
			Store.DataSource = Data;
			Store.DataBind();
			SelectedCouseStore.DataSource = from course in student.CourseList
											select new
											{
												tag = course.Tag.ToString(),
												tId = course.TId,
												courseId = course.CourseId,
												credit = course.Credit,
												courseName = course.CourseName,
												teacherName = course.TeacherName,
												time = course.Time,
												dept = course.Department,
												campus = course.Campus,
												place = course.Place,
												capacity = course.Capacity
											};
			SelectedCouseStore.DataBind();
			//SelectedCouseStore.Update();
			txtSchedule.Text = new string(buffer);
		}

		protected void BtnRefreshClick(object sender, DirectEventArgs e)
		{
			if (Session["UserId"] == null)
			{
				Response.Redirect(Constants.ERROR_PAGE);
			}
			else
			{
				student = new Model.Student(Session["UserId"].ToString(), txtTermYear.Text + txtTerm.Text);
				UpdateSchedule();
			}
		}
	}
}